/* Yet Another Forum.net
 * Copyright (C) 2006-2010 Jaben Cargman
 * http://www.yetanotherforum.net/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

namespace YAF.Classes.Core
{
  #region Using

  using System;

  using YAF.Classes.Pattern;
  using YAF.Classes.Utils;

  #endregion

  /// <summary>
  /// The yaf date time.
  /// </summary>
  public class YafDateTime
  {
    #region Constants and Fields

    /// <summary>
    ///   Time zone suffix for Guests
    /// </summary>
    private readonly string timeZoneName = YafContext.Current.Localization.GetText("TIMEZONES", "NAME_UTC");

    #endregion

    #region Properties

    /// <summary>
    ///   Gets the time zone offset 
    ///   for the current user.
    /// </summary>
    public TimeSpan TimeOffset
    {
      get
      {
        if (YafContext.Current.Page != null)
        {
          int min = YafContext.Current.TimeZoneUser;
          int hrs = min / 60;

          return new TimeSpan(hrs, (min % 60) + YafContext.Current.BoardSettings.ServerTimeCorrection, 0);
        }

        return new TimeSpan(0, YafContext.Current.BoardSettings.ServerTimeCorrection, 0);
      }
    }

    #endregion

    #region Public Methods

    /// <summary>
    /// Formats a datetime value into 7. february 2003
    /// </summary>
    /// <param name="dateTime">
    /// The date to be formatted
    /// </param>
    /// <returns>
    /// The format date long.
    /// </returns>
    public string FormatDateLong(DateTime dateTime)
    {
      string strDateFormat;
      dateTime = this.AccountForDST(dateTime + this.TimeOffset);

      try
      {
        strDateFormat =
          YafContext.Current.Localization.FormatDateTime(
            YafContext.Current.Localization.GetText("FORMAT_DATE_LONG"), dateTime);
      }
      catch (Exception)
      {
        strDateFormat = dateTime.ToString("D");
      }

      return YafContext.Current.IsGuest ? "{0}{1}".FormatWith(strDateFormat, this.timeZoneName) : strDateFormat;
    }

    /// <summary>
    /// Formats a datetime value into 07.03.2003
    /// </summary>
    /// <param name="dateTime">
    /// The date Time.
    /// </param>
    /// <returns>
    /// Short formatted date.
    /// </returns>
    public string FormatDateShort([NotNull] DateTime dateTime)
    {
      string strDateFormat;
      dateTime = this.AccountForDST(dateTime + this.TimeOffset);

      try
      {
        strDateFormat =
          YafContext.Current.Localization.FormatDateTime(
            YafContext.Current.Localization.GetText("FORMAT_DATE_SHORT"), dateTime);
      }
      catch (Exception)
      {
        strDateFormat = dateTime.ToString("d");
      }

      return YafContext.Current.IsGuest ? "{0}{1}".FormatWith(strDateFormat, this.timeZoneName) : strDateFormat;
    }

    /// <summary>
    /// Formats a datetime value into 07.03.2003 22:32:34
    /// </summary>
    /// <param name="dateTime">
    /// The date Time.
    /// </param>
    /// <returns>
    /// Formatted  <see cref="string"/> of the formatted <see cref="DateTime"/> Object.
    /// </returns>
    public string FormatDateTime([NotNull] DateTime dateTime)
    {
      dateTime = this.AccountForDST(dateTime + this.TimeOffset);

      string strDateFormat = "{0:F}".FormatWith(dateTime);

      try
      {
        strDateFormat =
          YafContext.Current.Localization.FormatDateTime(
            YafContext.Current.Localization.GetText("FORMAT_DATE_TIME_LONG"), dateTime);
      }
      catch (Exception)
      {
      }

      return YafContext.Current.IsGuest ? "{0}{1}".FormatWith(strDateFormat, this.timeZoneName) : strDateFormat;
    }

    /// <summary>
    /// This formats a DateTime into a short string
    /// </summary>
    /// <param name="dateTime">
    /// The date Time.
    /// </param>
    /// <returns>
    /// The formatted string created from the DateTime object.
    /// </returns>
    public string FormatDateTimeShort([NotNull] DateTime dateTime)
    {
      string strDateFormat;

      dateTime = this.AccountForDST(dateTime + this.TimeOffset);

      try
      {
        strDateFormat =
          YafContext.Current.Localization.FormatDateTime(
            YafContext.Current.Localization.GetText("FORMAT_DATE_TIME_SHORT"), dateTime);
      }
      catch (Exception)
      {
        strDateFormat = dateTime.ToString("f");
      }

      return YafContext.Current.IsGuest ? "{0}{1}".FormatWith(strDateFormat, this.timeZoneName) : strDateFormat;
    }

    /// <summary>
    /// Formats a datatime value into 07.03.2003 00:00:00 except if 
    ///   the date is yesterday or today -- in which case it says that.
    /// </summary>
    /// <param name="dateTime">
    /// The date Time.
    /// </param>
    /// <returns>
    /// Formatted string of DateTime object
    /// </returns>
    public string FormatDateTimeTopic([NotNull] DateTime dateTime)
    {
      dateTime = this.AccountForDST(dateTime + this.TimeOffset);
      DateTime nowDateTime = this.AccountForDST(DateTime.UtcNow + this.TimeOffset);

      try
      {
        string strDateFormat;

        if (dateTime.Date == nowDateTime.Date)
        {
          // today
          strDateFormat =
            YafContext.Current.Localization.FormatString(YafContext.Current.Localization.GetText("TodayAt"), dateTime);
        }
        else if (dateTime.Date == nowDateTime.AddDays(-1).Date)
        {
          // yesterday
          strDateFormat =
            YafContext.Current.Localization.FormatString(
              YafContext.Current.Localization.GetText("YesterdayAt"), dateTime);
        }
        else
        {
          strDateFormat =
            YafContext.Current.Localization.FormatDateTime(
              YafContext.Current.Localization.GetText("FORMAT_DATE_TIME_SHORT"), dateTime);
        }

        return YafContext.Current.IsGuest ? "{0}{1}".FormatWith(strDateFormat, this.timeZoneName) : strDateFormat;
      }
      catch (Exception)
      {
        return dateTime.ToString("f");
      }
    }

    /// <summary>
    /// Formats a datetime value into 22:32:34
    /// </summary>
    /// <param name="dateTime">
    /// The date to be formatted
    /// </param>
    /// <returns>
    /// The format time.
    /// </returns>
    public string FormatTime(DateTime dateTime)
    {
      string strDateFormat;

      dateTime = this.AccountForDST(dateTime + this.TimeOffset);

      try
      {
        strDateFormat =
          YafContext.Current.Localization.FormatDateTime(
            YafContext.Current.Localization.GetText("FORMAT_TIME"), dateTime);
      }
      catch (Exception)
      {
        strDateFormat = dateTime.ToString("T");
      }

      return YafContext.Current.IsGuest ? "{0}{1}".FormatWith(strDateFormat, this.timeZoneName) : strDateFormat;
    }

    #endregion

    #region Methods

    /// <summary>
    /// Determines if the given date falls during DST and updates accordingly if the user chosen to allow for DST.
    /// </summary>
    /// <param name="dtCurrent">
    /// The date to be checked
    /// </param>
    /// <returns>
    /// DateTime object account for DST (if required).
    /// </returns>
    private DateTime AccountForDST(DateTime dtCurrent)
    {
      if (YafContext.Current.DSTUser)
      {
        if (TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").IsDaylightSavingTime(dtCurrent))
        {
          return dtCurrent.AddHours(1);
        }
      }

      return dtCurrent;
    }

    #endregion
  }
}